class ArticlesController < ApplicationController
  
  before_action :set_article, only: [:edit, :show, :update, :destroy]

  def index
    @articles = current_user.articles
  end

  def new
    @article = Article.new
  end

  def edit
    if params[:modification_id].present?
      @modification = @article.modifications.find(params[:modification_id])
      @article.title = @modification.title
      @article.content = @modification.content
    end
  end

  def create
    @article = current_user.articles.new(article_params)

    if @article.save
      redirect_to @article
    else
      redirect_to :back
    end
  end

  def show
  end

  def update
    @article.assign_attributes(article_params)

    if @article.save
      redirect_to @article
    else
      redirect_to :back
    end
  end

  def destroy
    if @article.destroy
      redirect_to :root
    end
  end

  private

  def article_params
    params.require(:article).permit(:id, :title, :content)
  end

  def set_article
    @article = current_user.articles.find(params[:id])
  end

end
